var util = require('util');

/**
 * 
 * @fileOverview View control for posting port changes. On success, redirects
 *               back to edit port.
 * @author jmanero
 * 
 */

/** @ignore */
exports.callback = function(req, res) {
	switch (req.body.action) {
	case 'add':
		var port = new model.Port();
		port.device = req.body.device;
		port.name = req.body.name;
		port.id = req.body.id;
		port.type = req.body.type;
		port.direction = req.body.direction;
		port.description = req.body.description;
		port.save(function(e) {
			model.Message.error(e);
			res.redirect('https://' + req.header('X-Request-Host') + '/ports/'
					+ req.body._id);
		});
		break;
	case 'update':
		model.Port.update({
			_id : req.body._id
		}, {
			name : req.body.name,
			type : req.body.type,
			direction : req.body.direction,
			description : req.body.description
		}, function(e) {
			model.Message.error(e);
			res.redirect('https://' + req.header('X-Request-Host') + '/ports/'
					+ req.body._id);
		});
		break;
	case 'remove':
		model.Port.remove({
			_id : req.params._id
		}, function(e) {
			model.Message.error(e);
			res.redirect('https://' + req.header('X-Request-Host') + '/ports');
		});
	}
};

/** @ignore */
exports.path = '/ports';

/** @ignore */
exports.method = 'post';
